package com.lanou3g.ptp.base.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.base.mapper.BaseMapper;
import com.lanou3g.ptp.base.service.BaseService;
import com.lanou3g.ptp.entity.Base;
import com.lanou3g.ptp.vo.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.List;

@Service(timeout = 50000)
public class BaseServiceImpl implements BaseService {

    @Autowired
    private BaseMapper baseMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${REDIS_BASE_KEY_PREFIX}")
    private String REDIS_BASE_KEY_PREFIX;

    @Value("${REDIS_BASE_KEY_EXPIRE}")

    private Long REDIS_BASE_KEY_EXPIRE;

    //查询所有
    @Override
    public PageInfo<Base> findAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Base> all = baseMapper.findAll();
        return new PageInfo<>(all);
    }

    //根据名字模糊查询
    @Override
    public PageInfo<Base> findByKeyword(Integer page, Integer size, String keyword) {
        PageHelper.startPage(page, size);
        List<Base> byKeyword = baseMapper.findByKeyword("%" + keyword + "%");
        return new PageInfo<>(byKeyword);
    }

    @Override
    public void insert(Base base) {
        Date date = new Date();
        base.setStatus(0);
        base.setCreateTime(date);
        base.setUpdateTime(date);
        baseMapper.insert(base);
    }

    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        baseMapper.deleteByIds(ids);
    }

    @Override
    public void updateById(Base base) {
        base.setUpdateTime(new Date());
        baseMapper.updateById(base);
    }

    @Override
    public Base findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<Base> findByName(String keyword) {
        return baseMapper.findByKeyword(keyword);
    }

    @Override
    public List<Option.BaseOption> findOptions() {
        return this.baseMapper.findOptions();
    }

    @Override
    public String[] findNames() {
        List<Option.BaseOption> options = baseMapper.findOptions();
        int size = options.size();
        String[] baseNames = new String[size];
        for (int i = 0; i < size; i++) {
            baseNames[i] = options.get(i).getName();
        }
        return baseNames;
    }
}
